#!/usr/bin/env bash

#1:git home directory
git_commit()
{
    local ret=1
    local git_commit_cur_dir=`pwd`
    need_commit_dir=$1
    cd $need_commit_dir
    need_commit_dir=`pwd`
    res=`git status -s`
    if [ ${#res} -ge 2 ];then
        echo "-- git_commit directory is: $need_commit_dir"
        current_branch=`git branch  | grep "*"| cut -d ' ' -f 2`
        git branch -a | grep -E "remotes"|grep -E "$current_branch">/dev/null
        if [ $? -ne 0 ]; then
            if [ -n "$2" -a -n "$3" ] ; then
                git push $2 $3
            else
                git push origin $current_branch
            fi
            ret=0
        fi
        remote_rep=`git config --get branch.${current_branch}.remote`
        if [ -z $remote_rep ]; then
            git branch --set-upstream-to=origin/$current_branch 
            remote_rep=`git config --get branch.${current_branch}.remote`
        fi

        if [ -d ./.git ];then
            git add *
            git commit -m"`date` `hostname`"

            if [ -n "$2" -a -n "$3" ] ; then
                git push $2 $3
            else
                git push $remote_rep $current_branch 
            fi
            ret=0
        else
            echo "-- error, $need_commit_dir/.git does not exist."
        fi
    else
        echo "-- info,git need not to commit,$need_commit_dir has not been modified!"

        if [ -n "$2" -a -n "$3" ] ; then
            git push $2 $3
        else
            git push  
        fi
            ret=0
    fi

    cd $git_commit_cur_dir
    return $ret
}

svn_commit()
{
    if [ -z $2 ];then
        echo "-- error,please set id of svn commit"
        exit
    fi
    svn_commit_cur_dir=`pwd`
    need_commit_dir=$1
    cd $need_commit_dir
    need_commit_dir=`pwd`
    res=`svn st`
    if [ ${#res} -ge 2 ];then
        echo "-- svn_commit directory is: $need_commit_dir"
        svn st
        msg="$2 commit by svn_commit/tool_sh"
        svn ci -m "$msg"
    else
        echo "-- info,svn need not to commit,$need_commit_dir has not been modified!"
    fi
    cd $svn_commit_cur_dir
}
checkout_into_specific_branch()
{
    current_branch_tmp=`git branch  | grep "*"| cut -d ' ' -f 2`
    if [ $current_branch_tmp != $current_branch ];then
        echo "-- git checkout $current_branch"
        git checkout $current_branch
    fi
}

pull_out_code()
{
    pull_out_code_cur_dir=$1
    checkout_into_specific_branch
    #if current branch does not trach remote branch
    remote_rep=`git config --get branch.${current_branch}.remote`
    if [ -z "$remote_rep" ]; then
        git branch --set-upstream-to=$remote_repo_param/$current_branch 
        remote_rep=`git config --get branch.${current_branch}.remote`
    fi


    echo "-- git pull_out_code $pull_out_code_cur_dir"
    git pull
}

#1:dir
pull_out_code_simple()
{
    cur_dir_pull_out_code_simple=`pwd`
    cd $1
    echo "-- git pull_out_code_simple $cur_dir_pull_out_code_simple"
    git pull 
    cd $cur_dir_pull_out_code_simple
}
